home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c
- Subject: Re: malloc question
- Date: 10 Mar 1996 13:29:40 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4hvaj4$laj@solutions.solon.com>
- References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de> <314318AF.30F@iperbole.bologna.it>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <314318AF.30F@iperbole.bologna.it>,
- Enrico Persiani <vos0225@iperbole.bologna.it> wrote:
- >Error?!? Why do you think that casting is an error? The standard ANSI
- >runtime library says:
-
- He didn't say it was an error, he said it could *hide* an error.
-
- >So, if he needs to use that pointer returned by malloc function he have to
- >convert it from a 'void' pointer to a 'item' pointer ( PITEM ).
-
- Bullshit. The only time the cast would be necessary is in the args to
- a variadic function which does not expect a (void *).
-
- >Many compilers don't need the use of casting. It's only a good programming
- >style.
-
- No compiler can require it, and it is arguably *bad* style.
-
- >But remember: malloc returns a 'void' pointer because it doesn't know
- >wich kind of data you'll store in the allocated mem block!
-
- True.
-
- >C++ compilers are more rigorous! But if you write a strict-ANSI C program a
- >c++ compiler won't return any error...
-
- Bullshit.
-
- #include <stdlib.h>
-
- int
- main(void) {
- char *s = malloc(10);
- return 0;
- }
-
- is *completely* legal ANSI, and *CANNOT* be rejected by any legal C compiler.
- It *MUST* be diagnosed by a C++ compiler. (Every C++ compiler I've ever heard
- of would reject it, but of course, an implementation may *accept* any program,
- as long as there is a diagnostic.)
-
- C++ changes the semantics of (void *).
-
- To rehast, the error that the cast hides is that:
- /* note *no* inclusion of <stdlib.h> */
- int main(void) {
- char *s = malloc(10);
- return 0;
- }
- *must* get a warning, and
- /* note *no* inclusion of <stdlib.h> */
- int main(void) {
- char *s = (char *) malloc(10);
- return 0;
- }
- is not required to, because, as Kurt correctly stated, *the cast can hide
- a potential error*.
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-